package cn.wolfcode.luowowo.website.web;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;


    @RequestMapping("")
    public String index(Model model){
        List<Region> regions =regionService.queryHotRegions();
        model.addAttribute("hotRegions",regions);
        return "destination/index";
    }


    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId,Model model){
        //查询所有的目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        List<Destination> leftDests = new ArrayList<>();
        List<Destination> rightDests = new ArrayList<>();
        if(list!=null && list.size()>0){
            int min = list.size()/2;
            for (int i = 0;i<list.size();i++) {
                if(i<min){
                    leftDests.add(list.get(i));
                }else {
                    rightDests.add(list.get(i));
                }
            }
        }
        //页面左边数据
        model.addAttribute("leftDests",leftDests);
        //页面右边数据
        model.addAttribute("rightDests",rightDests);
        //第一次进入默认展示
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Long id,Model model){
        //toasts  吐司操作
        List<Destination> toasts = destinationService.getToasts(id);
        //最后的目的地单独处理,吐司多余一个,所以我们删除最后一个
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        //dest 吐司里有目的地内容,只需从里边取就可以了
        model.addAttribute("dest",dest);
        List<StrategyCatalog> catalogByStrategyId = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs",catalogByStrategyId);

        //strategyDetails
        List<StrategyDetail> list = strategyDetailService.getViewnumTop3(id);
        model.addAttribute("strategyDetails",list);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts  吐司操作
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //最后的目的地单独处理,吐司多余一个,所以我们删除最后一个
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        //dest 吐司里有目的地内容,只需从里边取就可以了
        model.addAttribute("dest",dest);

        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        StrategyCatalog catalog = null;
        if(catalogs!=null && catalogs.size()>0){
            //catalog
            for (StrategyCatalog c : catalogs) {
                if(c.getId()==qo.getCatalogId()){
                    //也面选择的分类
                    catalog=c;
                    model.addAttribute("catalog",c);
                }
            }
        }
        model.addAttribute("catalogs",catalogs);
        //detail第一篇攻略
        if(catalog!=null){
            List<StrategyDetail> details = catalog.getDetails();
            if(details!=null&&details.size()>0){
                StrategyDetail detail = strategyDetailService.get(details.get(0).getId());
                model.addAttribute("detail",detail);
            }
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public  String travels(Model model , @ModelAttribute("qo") TravelQuery qo){

        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
}
